using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entities;
namespace DAL
{
   public class tbTourDAL
    {
       private SqlConnection cn = null;
        public tbTourDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(tbTour t)
        {
            SqlCommand cm = new SqlCommand("sp_tour_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@maloaitour", t.Maloaitour);
            cm.Parameters.AddWithValue("@tentour", t.Tentour);
            cm.Parameters.AddWithValue("@thoigian", t.Thoigian);
            cm.Parameters.AddWithValue("@phuongtien", t.Phuongtien);
            cm.Parameters.AddWithValue("@gia", t.Gia);
            cm.Parameters.AddWithValue("@khoihanh", t.Khoihanh);
            cm.Parameters.AddWithValue("@mota", t.Mota);
            cm.Parameters.AddWithValue("@hinh", t.Hinh);
            cm.ExecuteNonQuery();
        }
        public void Update(tbTour t)
        {
            SqlCommand cm = new SqlCommand("sp_tour_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@matour ", t.Matour);
            cm.Parameters.AddWithValue("@maloaitour", t.Maloaitour);
            cm.Parameters.AddWithValue("@tentour", t.Tentour);
            cm.Parameters.AddWithValue("@thoigian", t.Thoigian);
            cm.Parameters.AddWithValue("@phuongtien", t.Phuongtien);
            cm.Parameters.AddWithValue("@gia", t.Gia);
            cm.Parameters.AddWithValue("@khoihanh", t.Khoihanh);
            cm.Parameters.AddWithValue("@mota", t.Mota);
            cm.Parameters.AddWithValue("@hinh", t.Hinh);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("sp_tour_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@matour", iD);
            cm.ExecuteNonQuery();
        }
        public tbTourCollection Get(string where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_tour_Get_2000", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbTourCollection tColl = new tbTourCollection();

            while (dr.Read()) 
            {
                tbTour t = new tbTour();
                t.Matour = int.Parse(dr.GetValue(0).ToString());
                t.Maloaitour = int.Parse(dr.GetValue(1).ToString());
                t.Tentour= dr.GetValue(2).ToString();
                t.Thoigian = dr.GetValue(3).ToString();
                t.Phuongtien = dr.GetValue(4).ToString();
                t.Gia=float.Parse(dr.GetValue(5).ToString());
                t.Khoihanh = dr.GetValue(6).ToString();
                t.Mota = dr.GetValue(7).ToString();
                t.Hinh = dr.GetValue(8).ToString();
                tColl.Add(t);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return tColl;
        }
    }
}
